package service.Impl;

import dao.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import service.IUserService;
import utils.SqlSessionUtil;
import vo.User;

import java.sql.SQLException;

public class IUserServiceImpl implements IUserService {
    SqlSession sqlSession = new SqlSessionUtil().sqlSession;

    @Override
    public User findUserById(int id) throws SQLException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUser(id);
        return user;
    }
    /*
    * @description: 注册后将user信息加入表格
    * @param [user]
    * @return void
    */
    @Override
    public void register(User user) throws SQLException {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) throws SQLException {

    }

    /*
    * @description:获取登陆user并在数据库中检索核对登陆信息
    * @param [user]
    * @return vo.User
    */
    @Override
    public User login(User user) throws SQLException {
        int id=user.getId();
        String password=user.getPassword();
        User user1=findUserById(id);
        if(user1==null){
            return null;
        }
        else if(user1.getPassword().equals(password)){
            return user;
        }
        else{
            return null;
        }
    }

}
